package dangnhap;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Random;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingConstants;
import javax.swing.SwingWorker;
import javax.swing.border.EmptyBorder;

import trangchu.TrangChu;

public class ProgressBarDemo extends JDialog implements ActionListener,
    PropertyChangeListener {

  private JProgressBar progressBar;
  private Task task;
  private JPanel contentPane;
  int progress = 0;
  private JLabel lblLoaddata;
  public FromDangNhap dangnhap = new FromDangNhap();

  class Task extends SwingWorker<Void, Void> {
    @Override
    public Void doInBackground() {
      Random random = new Random();
      
      setProgress(0);
      while (progress < 100) {
        try {
          Thread.sleep(random.nextInt(10));
        } catch (InterruptedException ignore) {
        }
        progress += random.nextInt(10);
        setProgress(Math.min(progress, 100));
      }
      return null;
    }

    @Override
    public void done() {
      Toolkit.getDefaultToolkit().beep();
      setCursor(null);
      dispose();
      new TrangChu(dangnhap).setVisible(true);
    }
  }

  public ProgressBarDemo() {
	  setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
		this.setTitle("Phần mềm quản lý bán hàng.");
		Image icon = Toolkit.getDefaultToolkit().getImage(FromDangNhap.rootPath+"images/TrangChu.gif");
		this.setIconImage(icon); 
		java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
	    setBounds((screenSize.width-502)/2, (screenSize.height-100)/2, 502, 106);
		this.setModal(true);
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(5, 5));
		getContentPane().add(contentPane);
		
		progressBar = new JProgressBar(0,100);
		progressBar.setValue(0);
	    progressBar.setStringPainted(true);
		contentPane.add(progressBar, BorderLayout.CENTER);
		
		JLabel lblXinChao = new JLabel("Chào mừng bạn đến với phần mềm quản lý bán hàng ShopM.");
		lblXinChao.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblXinChao.setForeground(Color.BLUE);
		lblXinChao.setHorizontalAlignment(SwingConstants.CENTER);
		contentPane.add(lblXinChao, BorderLayout.NORTH);
		lblLoaddata = new JLabel("Đang load dữ liệu ...");
		lblLoaddata.setHorizontalAlignment(SwingConstants.CENTER);
		contentPane.add(lblLoaddata, BorderLayout.SOUTH);
	    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
	    task = new Task();
	    task.addPropertyChangeListener(this);
	    task.execute();

  }



  public void actionPerformed(ActionEvent evt) {
  }

  public void propertyChange(PropertyChangeEvent evt) {
    if ("progress" == evt.getPropertyName()) {
      int progress = (Integer) evt.getNewValue();
      progressBar.setValue(progress);
    }
  }

  public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ProgressBarDemo frame = new ProgressBarDemo();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}